package com.controller;

/**
 * @author mornd
 * @date 2020/11/16 - 10:25
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Department;
import com.service.DepartmentService;
import com.service.UserService;
import com.util.ConstUtils;
import com.vo.DataVO;
import com.vo.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@RolesAllowed({"ROLE_ADMIN", "ROLE_SUPERADMIN"})
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;

    //查询部门
    @RequestMapping("/list")
    public DataVO<Department> findDept(DepartmentVo departmentVo) {
        PageHelper.startPage(departmentVo.getPage(), departmentVo.getLimit());
        PageInfo<Department> pageInfo = new PageInfo<Department>(departmentService.findDepartment(departmentVo));
        return new DataVO<Department>(pageInfo.getTotal(), pageInfo.getList());
    }

    //添加部门
    @RequestMapping("/add")
    public Map<String, Object> addDept(Department department) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (departmentService.addDepartment(department) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功!");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    //修改
    @RequestMapping("/update")
    public Map<String, Object> updateDept(Department department) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (departmentService.updateDepartment(department) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    //检查部门下是否有员工
    @RequestMapping("/checkDeptByUser")
    public Map<String, Object> checkDeptByUser(Integer deptId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (userService.checkDeptByUser(deptId) > 0) {
            maps.put(ConstUtils.EXIST, true);
            maps.put(ConstUtils.MESSAGE, "该部门下已有员工，不可删除！");
        } else {
            maps.put(ConstUtils.EXIST, false);
        }
        return maps;
    }

    //删除
    @RequestMapping("/del")
    public Map<String, Object> delDept(Integer deptId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (departmentService.deleteDepartment(deptId) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }

    /**
     * 查询部门集合 byRedis
     *
     * @return
     */
    @RequestMapping("/findDeptList")
    public String findRoomTypeByRedis() {
        return departmentService.findDeptByRedis();
    }
}
